home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / macros.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  6.0 KB  |  232 lines

  1. /* $Id: macros.h,v 1.4 1997/02/03 20:31:26 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.2
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: macros.h,v $
  26.  * Revision 1.4  1997/02/03 20:31:26  brianp
  27.  * added DEFARRAY and UNDEFARRAY macros for BeOS
  28.  *
  29.  * Revision 1.3  1996/10/31 01:12:00  brianp
  30.  * removed (GLint) from FLOAT_TO_UINT() macro
  31.  *
  32.  * Revision 1.2  1996/09/15 01:49:44  brianp
  33.  * added #define NULL 0
  34.  *
  35.  * Revision 1.1  1996/09/13 01:38:16  brianp
  36.  * Initial revision
  37.  *
  38.  */
  39.  
  40.  
  41. /*
  42.  * A collection of useful macros.
  43.  */
  44.  
  45.  
  46. #ifndef MACROS_H
  47. #define MACROS_H
  48.  
  49.  
  50. #include <math.h>
  51.  
  52.  
  53.  
  54. /* Limits: */
  55. #define MAX_GLUSHORT    0xffff
  56. #define MAX_GLUINT    0xffffffff
  57.  
  58.  
  59.  
  60. /* Copy short vectors: */
  61.  
  62. #define COPY_3V( DST, SRC )    DST[0] = SRC[0];    \
  63.                 DST[1] = SRC[1];    \
  64.                 DST[2] = SRC[2];
  65.  
  66. #define COPY_4V( DST, SRC )    DST[0] = SRC[0];    \
  67.                 DST[1] = SRC[1];    \
  68.                 DST[2] = SRC[2];    \
  69.                 DST[3] = SRC[3];
  70.  
  71.  
  72. /* Assign scalers to short vectors: */
  73. #define ASSIGN_3V( V, V0, V1, V2 )  V[0] = V0;  V[1] = V1;  V[2] = V2;
  74.  
  75. #define ASSIGN_4V( V, V0, V1, V2, V3 )  V[0] = V0;    \
  76.                         V[1] = V1;    \
  77.                         V[2] = V2;    \
  78.                         V[3] = V3;
  79.  
  80.  
  81. /* Test if we're inside a glBegin / glEnd pair: */
  82. #define INSIDE_BEGIN_END(CTX)  ((CTX)->Primitive!=GL_BITMAP)
  83.  
  84.  
  85.  
  86. /* Absolute value (for Int, Float, Double): */
  87. #define ABSI(X)  ((X) < 0 ? -(X) : (X))
  88. #define ABSF(X)  ((X) < 0.0F ? -(X) : (X))
  89. #define ABSD(X)  ((X) < 0.0 ? -(X) : (X))
  90.  
  91.  
  92.  
  93. /* Round a floating-point value to the nearest integer: */
  94. #define ROUNDF(X)  ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
  95.  
  96.  
  97. /* Compute ceiling of integer quotient of A divided by B: */
  98. #define CEILING( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
  99.  
  100.  
  101. /* Clamp X to [MIN,MAX]: */
  102. #define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
  103.  
  104.  
  105. /* Min of two values: */
  106. #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
  107.  
  108.  
  109. /* MAX of two values: */
  110. #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
  111.  
  112.  
  113. /* Dot product of two 3-element vectors */
  114. #define DOT3( a, b )  ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] )
  115.  
  116.  
  117. /* Dot product of two 4-element vectors */
  118. #define DOT4( a, b )  ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3] )
  119.  
  120.  
  121. /* Normalize a 3-element vector to unit length */
  122. #define NORMALIZE_3V( a )  { GLfloat len;                \
  123.                              len = sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);    \
  124.                  if (len>0.0001) {                \
  125.                     GLfloat scale = 1.0F / len;        \
  126.                     a[0] *= scale;                \
  127.                                 a[1] *= scale;                \
  128.                                 a[2] *= scale;                \
  129.                  }                        \
  130.                            }
  131.  
  132.  
  133. /*
  134.  * Integer / float conversion for colors, normals, etc.
  135.  */
  136.  
  137. /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
  138. #define UBYTE_TO_FLOAT(B)    ((GLfloat) (B) * (1.0F / 255.0F))
  139.  
  140. /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
  141. #define FLOAT_TO_UBYTE(X)    ((GLubyte) (GLint) (((X)) * 255.0F))
  142.  
  143.  
  144. /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
  145. #define BYTE_TO_FLOAT(B)    ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
  146.  
  147. /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
  148. #define FLOAT_TO_BYTE(X)    ( (((GLint) (255.0F * (X))) - 1) / 2 )
  149.  
  150.  
  151. /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
  152. #define USHORT_TO_FLOAT(S)    ((GLfloat) (S) * (1.0F / 65535.0F))
  153.  
  154. /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
  155. #define FLOAT_TO_USHORT(X)    ((GLushort) (GLint) ((X) * 65535.0F))
  156.  
  157.  
  158. /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
  159. #define SHORT_TO_FLOAT(S)    ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
  160.  
  161. /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
  162. #define FLOAT_TO_SHORT(X)    ( (((GLint) (65535.0F * (X))) - 1) / 2 )
  163.  
  164.  
  165. /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
  166. #define UINT_TO_FLOAT(U)    ((GLfloat) (U) * (1.0F / 4294967295.0F))
  167.  
  168. /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
  169. #define FLOAT_TO_UINT(X)    ((GLuint) ((X) * 4294967295.0))
  170.  
  171.  
  172. /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
  173. #define INT_TO_FLOAT(I)        ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
  174.  
  175. /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
  176. /* causes overflow:
  177. #define FLOAT_TO_INT(X)        ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
  178. */
  179. /* a close approximation: */
  180. #define FLOAT_TO_INT(X)        ( (GLint) (2147483647.0 * (X)) )
  181.  
  182.  
  183.  
  184. /* Memory copy: */
  185. #ifdef SUNOS4
  186. #define MEMCPY( DST, SRC, BYTES) \
  187.     memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) )
  188. #else
  189. #define MEMCPY( DST, SRC, BYTES) \
  190.     memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) )
  191. #endif
  192.  
  193.  
  194. /* Memory set: */
  195. #ifdef SUNOS4
  196. #define MEMSET( DST, VAL, N ) \
  197.     memset( (char *) (DST), (int) (VAL), (int) (N) )
  198. #else
  199. #define MEMSET( DST, VAL, N ) \
  200.     memset( (void *) (DST), (int) (VAL), (size_t) (N) )
  201. #endif
  202.  
  203.  
  204. /* MACs and BeOS don't support static larger than 32kb, so... */
  205. #if defined(__QUICKDRAW__)
  206. #  define MAC_ALLOC NewPtr
  207. #  define MAC_FREE DisposePtr
  208. #  define DEFARRAY(TYPE,NAME,SIZE)  TYPE *NAME = (TYPE*)MAC_ALLOC(sizeof(TYPE)*SIZE)
  209. #  define UNDEFARRAY(NAME)          MAC_FREE((char*)NAME)
  210. #elif defined(__BEOS__)
  211. #  define DEFARRAY(TYPE,NAME,SIZE)  TYPE *NAME = malloc(sizeof(TYPE)*(SIZE))
  212. #  define UNDEFARRAY(NAME)          free(NAME)
  213. #else
  214. #  define DEFARRAY(TYPE,NAME,SIZE)  TYPE NAME[SIZE]
  215. #  define UNDEFARRAY(NAME)
  216. #endif
  217.  
  218.  
  219. /* Pi */
  220. #ifndef M_PI
  221. #define M_PI (3.1415926)
  222. #endif
  223.  
  224.  
  225. #ifndef NULL
  226. #define NULL 0
  227. #endif
  228.  
  229.  
  230.  
  231. #endif /*MACROS_H*/
  232.